Ajax working, new users get missions and new signup layout

jamesperet 11 years ago
parent
commit
0cce372da4

+ 18 - 9
app/assets/javascripts/missions.js.coffee

@@ -2,13 +2,22 @@
2 2
 # All this logic will automatically be available in application.js.
3 3
 # You can use CoffeeScript in this file: http://coffeescript.org/
4 4
 
5
-jQuery ->  $('form').on 'click', '.remove_fields', (event) ->    
6
-	$(this).prev('input[type=hidden]').val('1')    
7
-	$(this).closest('fieldset').hide()    
8
-	event.preventDefault()
5
+attachAddItemHandler = ->
6
+	jQuery ->  $('form').on 'click', '.remove_fields', (event) ->    
7
+		$(this).prev('input[type=hidden]').val('1')    
8
+		$(this).closest('fieldset').hide()    
9
+		event.preventDefault()
9 10
 	
10
-jQuery -> $('form').on 'click', '.add_fields', (event) ->  
11
-	time = new Date().getTime()  
12
-	regexp = new RegExp($(this).data('id'), 'g')  
13
-	$(this).before($(this).data('fields').replace(regexp, time))  
14
-	event.preventDefault()
11
+attachRemoveItemHandler = ->
12
+	jQuery -> $('form').on 'click', '.add_fields', (event) ->  
13
+		time = new Date().getTime()  
14
+		regexp = new RegExp($(this).data('id'), 'g')  
15
+		$(this).before($(this).data('fields').replace(regexp, time))  
16
+		event.preventDefault()
17
+	
18
+
19
+$(document).ready attachRemoveItemHandler
20
+$(document).on "page:load", attachRemoveItemHandler
21
+
22
+$(document).ready attachAddItemHandler
23
+$(document).on "page:load", attachAddItemHandler

+ 16 - 27
app/controllers/missions_controller.rb

@@ -12,7 +12,7 @@ class MissionsController < ApplicationController
12 12
     @missions = @user.mission_agent_invites.where(:status => 'accepted')
13 13
     @missions += @user.mission_agent_invites.where(:status => 'Completed')
14 14
     @mission_control = Mission.where(:owner => @user)
15
-    @mission_invites = @user.mission_agent_invites.where(:status => 'invited')
15
+    @mission_invites = @user.mission_agent_invites.where(:status => 'Invited')
16 16
   end
17 17
   
18 18
   def list
@@ -31,9 +31,14 @@ class MissionsController < ApplicationController
31 31
   # GET /missions/new
32 32
   def new
33 33
     @mission = Mission.new
34
+    @mission.owner = current_user
34 35
     @mission.save
35 36
     a = @mission.mission_agents.new
36 37
     a.save
38
+    @mission.mission_agents.build
39
+    @mission.mission_agents.each do |agent|
40
+      agent.mission_agent_steps.build
41
+    end
37 42
   end
38 43
 
39 44
   # GET /missions/1/edit
@@ -43,7 +48,7 @@ class MissionsController < ApplicationController
43 48
   # POST /missions
44 49
   # POST /missions.json
45 50
   def create
46
-    @mission = Mission.new(mission_params)
51
+    @mission = Mission.find(mission_params)
47 52
     @mission.status = 'Initializing'
48 53
     @mission.owner = current_user
49 54
     respond_to do |format|
@@ -62,30 +67,6 @@ class MissionsController < ApplicationController
62 67
   def update
63 68
     respond_to do |format|
64 69
       if @mission.update(mission_params)
65
-        @mission.assign_attributes(params[:mission_agents_attributes])
66
-        @mission.assign_attributes(params[:mission_agent_steps_attributes])
67
-        params[:mission][:mission_agents_attributes].values.each do |a|
68
-          if a[:mission_agent_steps_attributes] != nil
69
-            a[:mission_agent_steps_attributes].values.each do |s|
70
-              @step = MissionAgentStep.where(:id => s[:id]).first_or_create
71
-              if s[:_destroy] == 1.to_s
72
-                @step.destroy
73
-              else
74
-                @step.mission_agent_id = a[:id]
75
-                @step.description = s[:description]
76
-                @step.save
77
-              end
78
-              # Update Step Order
79
-              @steps = MissionAgentStep.order(:step => :asc).find_all_by_mission_agent_id(@step.mission_agent)
80
-              @step_number = 1
81
-              @steps.each do |step|
82
-                step.step = @step_number
83
-                step.save
84
-                @step_number = @step_number + 1
85
-              end
86
-            end
87
-          end
88
-        end
89 70
         format.html { redirect_to mission_control_path(@mission), notice: 'Mission was successfully updated.' }
90 71
         format.json { head :no_content }
91 72
       else
@@ -194,6 +175,14 @@ class MissionsController < ApplicationController
194 175
 
195 176
     # Never trust parameters from the scary internet, only allow the white list through.
196 177
     def mission_params
197
-      params.require(:mission).permit(:title, :description, :status, :agent_search_start, :agent_search_end, :mission_agents_attributes  => [:id, :mission_id, :description, :_destroy], :mission_agent_steps_attributes => [:id, :mission_agent_id, :description, :proof, :_destroy])
178
+      params.require(:mission).permit(:title, :description, :status, :agent_search_start, :agent_search_end, :mission_agents_attributes  => [:id, :mission_id, :description, :_destroy, :mission_agent_steps_attributes => [:id, :mission_agent_id, :description, :proof, :_destroy]] )
179
+    end
180
+    
181
+    def mission_agent_params
182
+        params.require(:mission_agent).permit(:id, :mission_id, :description, :_destroy, :mission_agent_steps_attributes => [:id, :mission_agent_id, :description, :proof, :_destroy])
183
+    end
184
+    
185
+    def mission_agent_step_params
186
+      params.require(:mission_agent_step).permit(:id, :mission_agent_id, :description, :proof, :_destroy)
198 187
     end
199 188
 end

+ 20 - 13
app/controllers/users/registrations_controller.rb

@@ -1,19 +1,26 @@
1
-class users::registrationsController < Devise::registrationsController
1
+class Users::RegistrationsController < Devise::RegistrationsController
2 2
   
3 3
   layout 'auth'
4 4
 
5
-  # def new
6
-  #   super
7
-  # end
5
+  def sign_up(resource_name, resource)
6
+      sign_in(resource_name, resource)
7
+      @mission_agents = MissionAgent.all
8
+      @mission_agents.each do |agent|
9
+        if agent.mission.status == "Launched"
10
+          if agent.mission_agent_invites.where(:status => 'Accepted').length == 0 && agent.mission_agent_invites.where(:status => 'Invited').length == 0
11
+            if agent.mission.mission_agents.where(:user => current_user).length == 0
12
+              invite = agent.mission_agent_invites.new(:user => current_user, :status => 'Invited', :mission_agent => agent)
13
+              invite.save
14
+              agent.update(:user => current_user)
15
+            end
16
+          end
17
+        end
18
+      end
19
+  end
8 20
 
9
-  # def create
10
-  #   super
11
-  # end
21
+  def after_sign_up_path_for(resource)
22
+    missions_path
23
+  end
24
+  
12 25
   
13
-  protected
14
-
15
-    def after_update_path_for(resource)
16
-      user_path(resource)
17
-    end
18
-    
19 26
 end

+ 3 - 0
app/controllers/users/sessions_controller.rb

@@ -1,4 +1,7 @@
1 1
 class users::sessionsController < Devise::sessionsController
2
+  
3
+  layout 'auth'
4
+  
2 5
   # def new
3 6
   #   super
4 7
   # end

+ 4 - 2
app/helpers/application_helper.rb

@@ -1,10 +1,12 @@
1 1
 module ApplicationHelper
2
+  
2 3
   def link_to_add_fields(name, f, association)
3 4
     new_object = f.object.send(association).klass.new    
4 5
     id = new_object.object_id    
5
-    fields = f.fields_for(association, new_object, index: id) do |builder|      
6
+    fields = f.fields_for(association, new_object, child_index: id) do |builder|      
6 7
       render(association.to_s.singularize + "_form", f: builder)    
7 8
     end    
8
-    link_to(name, '#', class: "add_fields", data: {id: id, fields: fields.gsub("\n", "")})  
9
+    link_to(name, '', class: "add_fields", data: {id: id, fields: fields.gsub("\n", "")})  
9 10
   end
11
+  
10 12
 end

+ 26 - 13
app/views/devise/registrations/new.html.erb

@@ -1,18 +1,31 @@
1
-<h2>Sign up</h2>
1
+<h2></h2>
2 2
 
3 3
 <%= simple_form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
4
-  <%= f.error_notification %>
4
+  
5
+<div class="span4 offset4 well" style="">
6
+	<div class="login-panel panel panel-default">
7
+	     <div class="panel-heading">
8
+	         <h3 class="panel-title">Sign up</h3>
9
+	     </div>
10
+	     <div class="panel-body">
11
+	         <form role="form">
12
+			    <%= f.error_notification %>
13
+			  <div class="form-inputs">
14
+			    <%= f.input :username, required:true, autofocus: true %>
15
+			    <%= f.input :email, required: true %>
16
+			    <%= f.input :password, required: true %>
17
+			    <%= f.input :password_confirmation, required: true %>
18
+			  </div>
19
+			  <div class="form-group">
20
+			    <%= f.button :submit, "Sign up" %>
21
+			    <%= render "devise/shared/links" %>
22
+			  </div>
23
+	         </form>
24
+	     </div>
25
+	 </div>
26
+</div>
5 27
 
6
-  <div class="form-inputs">
7
-    <%= f.input :username, required:true, autofocus: true %>
8
-    <%= f.input :email, required: true %>
9
-    <%= f.input :password, required: true %>
10
-    <%= f.input :password_confirmation, required: true %>
11
-  </div>
12
-
13
-  <div class="form-actions">
14
-    <%= f.button :submit, "Sign up" %>
15
-  </div>
28
+  
16 29
 <% end %>
17 30
 
18
-<%= render "devise/shared/links" %>
31
+

+ 4 - 2
app/views/devise/sessions/new.html.erb

@@ -6,6 +6,7 @@
6 6
 	     </div>
7 7
 	     <div class="panel-body">
8 8
 	         <form role="form">
9
+			    <%= bootstrap_flash %>
9 10
 	             <fieldset>
10 11
 	                     <%= f.input :email, required: true, autofocus: true, :input_html => {class: "input-block-level"} %>
11 12
 	                 <div class="form-group">
@@ -14,10 +15,11 @@
14 15
 	                 <%= f.input :remember_me, as: :boolean, inline_label: 'Remember me', label: false if devise_mapping.rememberable? %>
15 16
 	                 <!-- Change this to a button or input when using this as a form -->
16 17
 				  <div class="form-group">
17
-				  <%= f.button :submit, "Sign in" %><br>
18
+				  <%= f.button :submit, "Sign in" %>
19
+				  <%= render "devise/shared/links" %>
18 20
 				  </div>
19 21
 				  <div class="form-group" style="margin-top: 10px;">
20
-				  <%= render "devise/shared/links" %>
22
+				  
21 23
 			  	  </div>
22 24
 	             </fieldset>
23 25
 	         </form>

+ 6 - 6
app/views/devise/shared/_links.erb

@@ -1,25 +1,25 @@
1 1
 <%- if controller_name != 'sessions' %>
2
-  <%= link_to "Sign in", new_session_path(resource_name) %><br />
2
+  <%= link_to "Sign in", new_session_path(resource_name), :class => 'btn btn-link' %>
3 3
 <% end -%>
4 4
 
5 5
 <%- if devise_mapping.registerable? && controller_name != 'registrations' %>
6
-  <%= link_to "Sign up", new_registration_path(resource_name) %><br />
6
+  <%= link_to "Sign up", new_registration_path(resource_name), :class => 'btn btn-link' %>
7 7
 <% end -%>
8 8
 
9 9
 <%- if devise_mapping.recoverable? && controller_name != 'passwords' && controller_name != 'registrations' %>
10
-  <%= link_to "Forgot your password?", new_password_path(resource_name) %><br />
10
+  <%= link_to "Forgot your password?", new_password_path(resource_name), :class => 'btn btn-link btn-small pull-right', :style => 'padding-top: 5px;' %>
11 11
 <% end -%>
12 12
 
13 13
 <%- if devise_mapping.confirmable? && controller_name != 'confirmations' %>
14
-  <%= link_to "Didn't receive confirmation instructions?", new_confirmation_path(resource_name) %><br />
14
+  <%= link_to "Didn't receive confirmation instructions?", new_confirmation_path(resource_name), :class => 'btn btn-link' %>
15 15
 <% end -%>
16 16
 
17 17
 <%- if devise_mapping.lockable? && resource_class.unlock_strategy_enabled?(:email) && controller_name != 'unlocks' %>
18
-  <%= link_to "Didn't receive unlock instructions?", new_unlock_path(resource_name) %><br />
18
+  <%= link_to "Didn't receive unlock instructions?", new_unlock_path(resource_name), :class => 'btn btn-link' %>
19 19
 <% end -%>
20 20
 
21 21
 <%- if devise_mapping.omniauthable? %>
22 22
   <%- resource_class.omniauth_providers.each do |provider| %>
23
-    <%= link_to "Sign in with #{provider.to_s.titleize}", omniauth_authorize_path(resource_name, provider) %><br />
23
+    <%= link_to "Sign in with #{provider.to_s.titleize}", omniauth_authorize_path(resource_name, provider), :class => 'btn btn-link' %><br />
24 24
   <% end -%>
25 25
 <% end -%>

+ 0 - 1
app/views/layouts/auth.html.erb

@@ -57,7 +57,6 @@
57 57
     <div class="container top-container">
58 58
         <div class="row">
59 59
           <div class="span12">
60
-            <%= bootstrap_flash %>
61 60
             <%= yield %>
62 61
           </div>
63 62
         </div><!--/row-->

+ 1 - 1
app/views/missions/_mission_agent_form.html.erb

@@ -5,7 +5,7 @@
5 5
 	<%= f.text_area :description%><br/>
6 6
 	<%= f.hidden_field :_destroy%>
7 7
 	<%= link_to "remove", '#', class: "remove_fields"%>
8
-	<%= f.fields_for :mission_agent_steps, MissionAgentStep.where(:mission_agent_id => f.object.id).order(:step => :asc) do |step_builder| %>
8
+	<%= f.fields_for :mission_agent_steps, MissionAgentStep.where(:mission_agent_id => f.object.id).order(:step => :asc), show_empty: true do |step_builder| %>
9 9
 		<%= render 'mission_agent_step_form', f: step_builder %>
10 10
 	<%end%>
11 11
 	<%= link_to_add_fields "Add Step", f, :mission_agent_steps %>

+ 11 - 6
app/views/missions/_mission_agent_step_form.html.erb

@@ -1,8 +1,13 @@
1 1
 <fieldset>
2
-	<h4>Step <%= @step %></h4>
3
-	<%= f.label :description, "Description"%>
4
-	<%= f.text_field :description %>
5
-	<%= f.hidden_field :_destroy%>
6
-	<%= link_to "remove", '#', class: "remove_fields"%>
7
-	<% @step = @step + 1 %>
2
+	<% if f %>
3
+	    	<h4>Step <%= @step %></h4>
4
+	    	<%= f.label :description, "Description"%>
5
+	    	<%= f.text_field :description %>
6
+	    	<%= f.hidden_field :_destroy%>
7
+	    	<%= link_to "remove", '#', class: "remove_fields"%>
8
+	    	<% @step = @step + 1 %>
9
+	<% else %>
10
+	    <p class="empty">There are no steps.</p>
11
+	<% end %>
12
+	
8 13
 </fieldset>

+ 27 - 23
app/views/missions/show.html.erb

@@ -6,7 +6,9 @@
6 6
   <small>Director: <%= link_to @mission.owner.username, agent_profile_path(@mission.owner.id) %></small>
7 7
 </div>
8 8
 <p class="lead"><strong>Details: </strong><%= @mission.description %></p>
9
-<p class="lead"><strong>Objective: </strong><%= @agent.first.description %></p>
9
+<% if @agent.first != nil %>
10
+	<p class="lead"><strong>Objective: </strong><%= @agent.first.description %></p>
11
+<% end %>
10 12
 <ul class="thumbnails">
11 13
 <% @step = 1 %>
12 14
 <% if @agent.length > 0 %>
@@ -24,28 +26,30 @@
24 26
 <% end %>
25 27
 </ul>
26 28
 
27
-<% @step = 1 %>
28
-<% @agent.first.mission_agent_steps.order(:step => :asc).each do |step| %>
29
+<% if @agent.first != nil %>
30
+	<% @step = 1 %>
31
+	<% @agent.first.mission_agent_steps.order(:step => :asc).each do |step| %>
29 32
 
30
-<!-- Modal -->
31
-<div id="myModal<%= @step %>" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
32
-  <div class="modal-header">
33
-    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
34
-    <h3 id="myModalLabel">Validate Step <%= @step %> </h3>
35
-  </div>
36
-  <%= simple_form_for(step) do |f| %>
37
-	  <div class="modal-body">
38
-	      <%= f.error_notification %>
39
-	      <div class="form-inputs">
40
-		   <%= f.hidden_field :id %>
41
-	        <%= f.input :proof, :label => 'Answer', :input_html => { :class => 'span5'} %> 
42
-	      </div>
33
+	<!-- Modal -->
34
+	<div id="myModal<%= @step %>" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
35
+	  <div class="modal-header">
36
+	    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
37
+	    <h3 id="myModalLabel">Validate Step <%= @step %> </h3>
43 38
 	  </div>
44
-	  <div class="modal-footer">
45
-	    <button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
46
-	     <%= f.button :submit, 'Validate', :class => 'btn btn-success' %> 
47
-	  </div>
48
-   <% end %>
49
-</div>
50
-<% @step = @step + 1 %>
39
+	  <%= simple_form_for(step) do |f| %>
40
+		  <div class="modal-body">
41
+		      <%= f.error_notification %>
42
+		      <div class="form-inputs">
43
+			   <%= f.hidden_field :id %>
44
+		        <%= f.input :proof, :label => 'Answer', :input_html => { :class => 'span5'} %> 
45
+		      </div>
46
+		  </div>
47
+		  <div class="modal-footer">
48
+		    <button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
49
+		     <%= f.button :submit, 'Validate', :class => 'btn btn-success' %> 
50
+		  </div>
51
+	   <% end %>
52
+	</div>
53
+	<% @step = @step + 1 %>
54
+	<% end %>
51 55
 <% end %>

+ 35 - 2
app/views/start/index.html.erb

@@ -2,7 +2,7 @@
2 2
    <div class="container">
3 3
        <div class="intro-text">
4 4
            <div class="intro-lead-in">
5
-			 So you want to become an agent?
5
+			 So you want to be an agent?
6 6
 		</div>
7 7
            <div class="intro-heading">
8 8
 			 Welcome to Avalanche
@@ -12,4 +12,37 @@
12 12
 		 <% end %>
13 13
        </div>
14 14
    </div>
15
-</header>
15
+</header>
16
+
17
+    <div class="container top-container">
18
+        <div class="row">
19
+          <div class="span12">
20
+			<blockquote class="">
21
+			  <p class="lead">Avanche is an online plataform where you play the role of an agent and gets assigned to real life missions.</p>
22
+			   <small>James Peret <cite title="Source Title">(Avalanche developer)</cite></small>
23
+			</blockquote>
24
+			<div class="clearfix"></div>
25
+			<div class="page-header">
26
+			  <h1>How it works</h1>
27
+			</div>
28
+		     <ul class="thumbnails">
29
+		   	  <li class="span4">
30
+		   		  <div class="thumbnail mission_thumb" >
31
+		   		  	<h3 style="margin-top: 5px;">Choose a mission</h3>
32
+		   		  </div>
33
+		   	  </li>
34
+		   	  <li class="span4">
35
+		   		  <div class="thumbnail mission_thumb" >
36
+		   		  	<h3 style="margin-top: 5px;">Execute all objectives</h3>
37
+		   		  </div>
38
+		   	  </li>
39
+		   	  <li class="span4">
40
+		   		  <div class="thumbnail mission_thumb" >
41
+		   		  	<h3 style="margin-top: 5px;">Complete the mission</h3>
42
+		   		  </div>
43
+		   	  </li>
44
+		     </ul>
45
+			
46
+          </div>
47
+        </div><!--/row-->
48
+    </div> <!-- /container -->

+ 1 - 1
config/application.rb

@@ -22,7 +22,7 @@ module AvalancheGame
22 22
     
23 23
     config.to_prepare do
24 24
         Devise::SessionsController.layout "auth"
25
-        Devise::RegistrationsController.layout proc{ |controller| user_signed_in? ? "application"   : "auth" }
25
+        Devise::RegistrationsController.layout proc{ |controller| user_signed_in? ? "auth"   : "auth" }
26 26
         Devise::ConfirmationsController.layout "auth"
27 27
         Devise::UnlocksController.layout "auth"            
28 28
         Devise::PasswordsController.layout "auth"        

+ 1 - 1
config/routes.rb

@@ -20,7 +20,7 @@ AvalancheGame::Application.routes.draw do
20 20
       post 'login' => 'devise/sessions#create', :as => :user_session
21 21
       delete 'logout' => 'devise/sessions#destroy', :as => :destroy_user_session
22 22
       get   'signup' => 'devise/registrations#new',    :as => :new_user_registration
23
-      post  'signup' => 'devise/registrations#create', :as => :user_registration
23
+      post  'signup' => 'users/registrations#create', :as => :user_registration
24 24
       put  'signup' => 'devise/registrations#update', :as => :user_registration_update
25 25
       scope '/account' do
26 26
           # password reset